package service

import (
	"fmt"
	"github.com/kevinlisr/install-levault/conf"
	"github.com/kevinlisr/install-levault/utils"
	"github.com/kevinlisr/install-levault/vari"
	"time"
)

type Installer struct{}

func NewInstaller() *Installer {
	return &Installer{}
}
func (i *Installer) DeployMiddle(pkgs []string, conf *conf.Config) error {
	fmt.Printf("********++++++++++++*******%s,==== %d\n", pkgs, len(pkgs))

	for _, pkg := range pkgs {
		if pkg != "" {
			switch pkg {
			case conf.Packages.Dm:
				vari.Logger.Info(fmt.Sprintf("start install %s", pkg))
				//fmt.Printf("start install %s\n", pkg)
				_, err := utils.RunSSHCommand("rpm", "-ivh", pkg)
				if err != nil {
					vari.Logger.Error("exec command failed, " + err.Error())
					//fmt.Println("exec command failed", err.Error())
					return fmt.Errorf("%s Install Failed, %s", pkg, err.Error())
				}
				vari.Logger.Info("install success, package is " + pkg)
				time.Sleep(time.Second * 3)

				_, err = utils.RunSSHCommand("systemctl", "start", "DmServiceLevault.service")
				if err != nil {
					return fmt.Errorf("%s Start Failed, %s", pkg, err.Error())
				}
			case conf.Packages.Minio:
				vari.Logger.Info(fmt.Sprintf("start install %s", pkg))
				_, err := utils.RunSSHCommand("rpm", "-ivh", pkg)
				if err != nil {
					return fmt.Errorf("%s Install Failed, %s", pkg, err.Error())
				}
				vari.Logger.Info(pkg + " install success!")
				time.Sleep(time.Second * 3)

				_, err = utils.RunSSHCommand("systemctl", "start", "minio.service")
				if err != nil {
					return fmt.Errorf("%s Start Failed, %s", pkg, err.Error())
				}
			case conf.Packages.Redis:
				vari.Logger.Info("start install: " + pkg)
				_, err := utils.RunSSHCommand("rpm", "-ivh", pkg)
				if err != nil {
					return fmt.Errorf("%s Install Failed, %s", pkg, err.Error())
				}
				vari.Logger.Info(pkg + " install success!")
				time.Sleep(time.Second * 3)

				_, err = utils.RunSSHCommand("systemctl", "start", "redis.service")
				if err != nil {
					return fmt.Errorf("%s Start Failed, %s", pkg, err.Error())
				}
			case conf.Packages.Rabbitmq:
				vari.Logger.Info("start install: " + pkg)
				_, err := utils.RunSSHCommand("rpm", "-ivh", pkg)
				if err != nil {
					return fmt.Errorf("%s Install Failed, %s", pkg, err.Error())
				}
				vari.Logger.Info(pkg + " install success!")
				time.Sleep(time.Second * 3)

				_, err = utils.RunSSHCommand("systemctl", "start", "rabbitmq.service")
				if err != nil {
					return fmt.Errorf("%s Start Failed, %s", pkg, err.Error())
				}
			case conf.Packages.Openresty:
				vari.Logger.Info("start install: " + pkg)
				_, err := utils.RunSSHCommand("rpm", "-ivh", pkg)
				if err != nil {
					return fmt.Errorf("%s Install Failed, %s", pkg, err.Error())
				}
				vari.Logger.Info(pkg + " install success!")
				time.Sleep(time.Second * 3)

				_, err = utils.RunSSHCommand("systemctl", "start", "openresty.service")
				if err != nil {
					return fmt.Errorf("%s Start Failed, %s", pkg, err.Error())
				}
				vari.Logger.Info(pkg + " Start success!")
			default:
				vari.Logger.Info("start install: " + pkg)
				_, err := utils.RunSSHCommand("rpm", "-ivh", pkg)
				if err != nil {
					return fmt.Errorf("%s Install Failed, %s", pkg, err.Error())
				}
				vari.Logger.Info(pkg + " install success!")
				time.Sleep(time.Second * 3)
			}

		}

		//fmt.Println("***************", pkg)
		//vari.Logger.Info(fmt.Sprintf("start install %s", pkg))
		//fmt.Printf("start install %s\n", pkg)
		//_, err := utils.RunSSHCommand("pwd")
		//if err != nil {
		//	return err
		//}
		//_, err = utils.RunSSHCommand("rpm", "-ivh", pkg)
		//if err != nil {
		//	vari.Logger.Error("exec command failed, " + err.Error())
		//	//fmt.Println("exec command failed", err.Error())
		//	return fmt.Errorf("%s Install Failed, %s", pkg, err.Error())
		//}
		//vari.Logger.Info("install success, package is " + pkg)
		//time.Sleep(time.Second * 3)
		//
		//_, err = utils.RunSSHCommand("systemctl", "start", "DmServiceLevault.service")
		//if err != nil {
		//	return fmt.Errorf("%s Start Failed, %s", pkg, err.Error())
		//}
	}
	fmt.Println("===================================    Dm    ===================================")
	fmt.Println("1. 修改达梦启动端口为5236")
	fmt.Println("/opt/nancal/base/dmdb/dmdata/data/Levault/dm.ini")
	fmt.Println("2. 重启达梦")
	fmt.Println("systemctl stop DmServiceLevault.service")
	fmt.Println("systemctl start DmServiceLevault.service")
	fmt.Println("===================================NebulaGraph===================================")
	fmt.Println("START NebulaGraph: ")
	fmt.Println("cd /data/nancal/nebula; scripts/nebula.service start all")
	fmt.Println("INIT NebulaGraph: ")
	fmt.Println("1. 登录：")
	fmt.Println("bin/nebula-console -u root -p 1 -P 9669 -addr 127.0.0.1")
	fmt.Println("2. 添加storage：")
	fmt.Println("add hosts 127.0.0.1:9779;")
	fmt.Println("3. 查看：")
	fmt.Println("show hosts;")
	fmt.Println("4. 初始化乐仓数据")
	fmt.Println("DROP SPACE IF EXISTS lzos;")
	fmt.Println("CREATE SPACE IF NOT EXISTS lzos (vid_type=INT64)  comment=\"lzos图空间\";")
	fmt.Println("DROP SPACE IF EXISTS lzos_0;")
	fmt.Println("CREATE SPACE IF NOT EXISTS lzos_0 (vid_type=INT64)  comment=\"lzos图空间\";")
	fmt.Println("5. 退出")
	fmt.Println("exit")
	fmt.Println("===================================   Nacos   ===================================")
	fmt.Println("START Nacos")
	fmt.Println("/opt/nancal/base/nacos/bin/startup.sh -m standalone")
	fmt.Println("如果启动报错 ERROR: Please set the JAVA_HOME variable in your environment, We need java(x64)! jdk8 or later is better! !!")
	fmt.Println("[ ! -e \"$JAVA_HOME/bin/java\" ] && JAVA_HOME=$HOME/jdk/java, 在这一行前边加上如下配置: ")
	fmt.Println("JAVA_HOME=/opt/nancal/jdk/jdk8")
	fmt.Println("===================================  RocketMQ  ===================================")
	fmt.Println("START RocketMQ")
	fmt.Println("cd /opt/nancal/base/rocketmq/")
	fmt.Println("1. 启动nameserver：")
	fmt.Println("nohup sh bin/mqnamesrv &")
	fmt.Println("2. 启动broker：")
	fmt.Println("nohup sh bin/mqbroker -c conf/broker.conf  -n 127.0.0.1:9876 &")
	return nil
}
